package com.ym.jiujiangeyou.util;

import java.io.File;



import java.io.IOException;
import com.ym.jiujiangeyou.app.MyApplication;


import android.util.Log;
/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class ConfigCache {

	private static final String TAG = ConfigCache.class.getName();

	public static final int CONFIG_CACHE_MOBILE_TIMEOUT = 7200000; // 2 hour
	public static final int CONFIG_CACHE_WIFI_TIMEOUT = 1800000; // 30 minute
	
	
	
	public static String getUrlCache(String url) {
		if (url == null) {
			return null;
		}

		String result = null;
		File file = new File(MyApplication.getInstance().getCacheDir()
				+ File.separator + Util.replaceUrlWithPlus(url));
		if (file.exists() && file.isFile()) {
			/*long expiredTime = System.currentTimeMillis() - file.lastModified();
			Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime
					/ 60000 + "min");
			// 1. in case the system time is incorrect (the time is turn back
			// long ago)
			// 2. when the network is invalid, you can only read the cache
			if (MyApplication.mNetWorkState != NetworkUtil.NETWORN_NONE
					&& expiredTime < 0) {
				return null;
			}
			if (MyApplication.mNetWorkState == NetworkUtil.NETWORN_WIFI
					&& expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) {
				return null;
			} else if (MyApplication.mNetWorkState == NetworkUtil.NETWORN_MOBILE
					&& expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) {
				return null;
			}*/
			try {
				result = FileManager.readTextFile(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	// 判断缓存是否过期   删除缓存
	public static boolean isExitCache(String url){
		File file = new File(MyApplication.getInstance().getCacheDir()
				+ File.separator + Util.replaceUrlWithPlus(url));
		if (file.exists() && file.isFile()) {
			long expiredTime = System.currentTimeMillis() - file.lastModified();
			Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime
					/ 60000 + "min");
			// 1. in case the system time is incorrect (the time is turn back
			// long ago)
			// 2. when the network is invalid, you can only read the cache
			if (MyApplication.mNetWorkState != NetworkUtil.NETWORN_NONE
					&& expiredTime < 0) {
				return true ;
			}
			if (MyApplication.mNetWorkState == NetworkUtil.NETWORN_WIFI
					&& expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) {
				return true ;
			} else if (MyApplication.mNetWorkState == NetworkUtil.NETWORN_MOBILE
					&& expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) {
				return true ;
			}
		}
		return false ;
	}
	
	//  过期删除缓存
	public static void delCache(String url){
		if(isExitCache(url)){
			File cacheDir = MyApplication.getInstance().getCacheDir() ;
			clearCache(cacheDir) ;
		}
	}
	
	/** 
	* @param data
	* @param url  
	* @retur  void 
	* @Description: 保存缓存 
	*/
	public static void setUrlCache(String data, String url) {
		if (MyApplication.getInstance().getCacheDir()
				+ File.separator == null) {
			return;
		}
		File file = new File(MyApplication.getInstance().getCacheDir()
				+ File.separator+ Util.replaceUrlWithPlus(url));
		if(file.exists()) 
			file.delete() ;
		try {
			// 创建缓存数据到磁盘，就是创建文件
			FileManager.writeTextFile(file, data);
		} catch (Exception e) {
			Log.d(TAG, "write " + file.getAbsolutePath() + " data failed!");
			e.printStackTrace();
		}
	}
	/** 
	* @param url
	* @return  
	* @retur  boolean 
	* @Description: 判断缓存文件是否存在 
	*/
	public static boolean isExitCacheFile(String url){
		File file = new  File(MyApplication.getInstance().getCacheDir()
				+ File.separator+ Util.replaceUrlWithPlus(url)) ;
		return file.exists() ;
	}
	
	/** 
	* @param cacheFile  
	* @retur  void 
	* @Description: 清除缓存文件
	*/
	public static void clearCache(File cacheFile) {
		if (cacheFile == null) {
			try {
				File cacheDir = MyApplication.getInstance().getCacheDir() ;
				File filesDir = MyApplication.getInstance().getFilesDir();
				if (cacheDir.exists()) {
					clearCache(cacheDir);
					clearCache(filesDir);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (cacheFile.isFile()) {
			cacheFile.delete();
		} else if (cacheFile.isDirectory()) {
			File[] childFiles = cacheFile.listFiles();
			for (int i = 0; i < childFiles.length; i++) {
				clearCache(childFiles[i]);			
			}	
		}
	}
	
}
